﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Crosland.Automotriz.Logic.Linq;
using Crosland.Automotriz.Entidad;
using DevExpress.Web.ASPxEditors;

namespace Crosland.Automotriz.Web.CRM
{
    public partial class wFrmClienteCartera : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false) 
            {
                ListarOrganizacion();
                cboOrganizacion_SelectedIndexChanged(null, null);
                cboCanal_SelectedIndexChanged(null, null);

                if (Request.QueryString["o"] != null) 
                {
                    int ID_ORG_VTA = int.Parse(Request.QueryString["o"].ToString());
                    int ID_CANAL_DISTRIB = int.Parse(Request.QueryString["c"].ToString());
                    int ID_VENDEDOR = int.Parse(Request.QueryString["v"].ToString());

                    int indiceOrganizacion = 0;
                    for (int i = 0; i < cboOrganizacion.Items.Count; i++) 
                    {
                        if (int.Parse(cboOrganizacion.Items[i].Value.ToString()) == ID_ORG_VTA) 
                        {
                            indiceOrganizacion = i;
                        }
                    }

                    cboOrganizacion.SelectedIndex = indiceOrganizacion;
                    cboOrganizacion_SelectedIndexChanged(null, null);

                    int indiceCanal = 0;
                    for (int x = 0; x < cboCanal.Items.Count; x++)
                    {
                        if (int.Parse(cboCanal.Items[x].Value.ToString()) == ID_CANAL_DISTRIB) 
                        {
                            indiceCanal = x;
                        }
                    }

                    cboCanal.SelectedIndex = indiceCanal;
                    cboCanal_SelectedIndexChanged(null, null);

                    int indiceSupervisor = 0;
                    for (int w = 0; w < cboSupervisor.Items.Count; w++) 
                    {
                        if (int.Parse(cboSupervisor.Items[w].Value.ToString()) == ID_VENDEDOR) 
                        {
                            indiceSupervisor = w;
                        }
                    }

                    cboSupervisor.SelectedIndex = indiceSupervisor;
                    cboSupervisor_SelectedIndexChanged(null, null);
                }

                Panel img = (Panel)Master.FindControl("asp");
                img.Visible = false;
            }
            Listar();
        }

        void ListarOrganizacion()
        {
            clsTd_Org_VtaLogic oOrg = new clsTd_Org_VtaLogic();
            cboOrganizacion.DataSource = oOrg.ListaOrgClienteVendedor();
            cboOrganizacion.TextField = "DES_ORG_VTA";
            cboOrganizacion.ValueField = "ID_ORG_VTA";
            cboOrganizacion.DataBind();
            cboOrganizacion.SelectedIndex = 0;
        }

        void ListarCanal()
        {
            int ID_ORG_VTA = int.Parse(cboOrganizacion.Value.ToString());
            clsTd_Canal_DistribLogic oCanal = new clsTd_Canal_DistribLogic();
            cboCanal.DataSource = oCanal.ListarCanalxOrganizacionClienteVendedor(ID_ORG_VTA);
            cboCanal.TextField = "DES_CANAL_DISTRIB";
            cboCanal.ValueField = "ID_CANAL_DISTRIB";
            cboCanal.DataBind();
            cboCanal.SelectedIndex = 0;
            if (cboCanal.Items.Count == 0)
            {
                cboCanal.Enabled = false;
                cboSupervisor.Enabled = false;
            }
            else
            {
                cboCanal.Enabled = true;
                cboSupervisor.Enabled = true;
            }
        }

        void ListarVendedor()
        {
            if (cboCanal.Enabled == true)
            {
                int ID_ORG_VTA = int.Parse(cboOrganizacion.Value.ToString());
                int ID_CANAL_DISTRIB = int.Parse(cboCanal.Value.ToString());

                clsTD_VendedorBL oVen = new clsTD_VendedorBL();

                cboSupervisor.DataSource = oVen.ListarVendedor_Cliente_Vendedor(ID_ORG_VTA, ID_CANAL_DISTRIB);
                cboSupervisor.TextField = "NOMBRE_APELLIDO";
                cboSupervisor.ValueField = "ID_VENDEDOR";
                cboSupervisor.DataBind();
                cboSupervisor.SelectedIndex = 0;

                Listar();
            }
        }

        protected void cboOrganizacion_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListarCanal();
            cboCanal_SelectedIndexChanged(null, null);
        }

        void Listar()
        {
            if (cboCanal.Enabled == true)
            {
                int ID_ORG_VTA = int.Parse(cboOrganizacion.Value.ToString());
                int ID_CANAL_DISTRIB = int.Parse(cboCanal.Value.ToString());
                int ID_VENDEDOR = int.Parse(cboSupervisor.Value.ToString());

                clsT_CLIENTE_VENDEDORBL ocv = new clsT_CLIENTE_VENDEDORBL();
                gv.DataSource = ocv.ListarClienteVendedor_x_Vendedor(ID_ORG_VTA, ID_CANAL_DISTRIB, ID_VENDEDOR);
                gv.DataBind();
            }
        }

        protected void gv_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            try
            {
                string PK = e.Keys[0].ToString();
                string[] arreglo = PK.Split(char.Parse("-"));
                int ID_ORG_VTA = int.Parse(arreglo[0].ToString());
                int ID_CANAL_DISTRIB = int.Parse(arreglo[1].ToString());
                int ID_CLIENTE = int.Parse(arreglo[2].ToString());

                T_CLIENTE_VENDEDOR ecv = new T_CLIENTE_VENDEDOR();
                ecv.ID_ORG_VTA = ID_ORG_VTA;
                ecv.ID_CANAL_DISTRIB = ID_CANAL_DISTRIB;
                ecv.ID_CLIENTE = ID_CLIENTE;

                clsT_CLIENTE_VENDEDORBL ocv = new clsT_CLIENTE_VENDEDORBL();
                ocv.Eliminar(ecv);
                e.Cancel = true;
                Listar();
            }
            catch (Exception)
            {

            }
        }

        protected void btnSalir_Click(object sender, EventArgs e)
        {
            Response.Redirect("wFrmDefault.aspx?id=24");
        }

        protected void cboCanal_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListarVendedor();
        }

        protected void cboSupervisor_SelectedIndexChanged(object sender, EventArgs e)
        {
            Listar();
        }

        protected void btnNuevo_Click(object sender, EventArgs e)
        {
            string ID_ORG_VTA = cboOrganizacion.SelectedItem.Value.ToString();
            string ID_CANAL_DISTRIB = cboCanal.SelectedItem.Value.ToString();
            string ID_VENDEDOR = cboSupervisor.SelectedItem.Value.ToString();

            Response.Redirect("wFrmClienteCarteraActualizar.aspx?o=" + ID_ORG_VTA + "&c=" + ID_CANAL_DISTRIB + "&v=" + ID_VENDEDOR);
        }

        protected void btnModificar_Click(object sender, EventArgs e)
        {
            int fila = gv.FocusedRowIndex;
            if (fila >= 0)
            {
                string ID_CLIENTE;
                ID_CLIENTE = gv.GetRowValues(fila, "ID_CLIENTE").ToString();
                string ID_ORG_VTA;
                ID_ORG_VTA = gv.GetRowValues(fila, "ID_ORG_VTA").ToString();
                string ID_CANAL_DISTRIB;
                ID_CANAL_DISTRIB = gv.GetRowValues(fila, "ID_CANAL_DISTRIB").ToString();
                string ID_VENDEDOR;
                ID_VENDEDOR = gv.GetRowValues(fila, "ID_VENDEDOR").ToString();

                Response.Redirect("wFrmClienteCarteraActualizar.aspx?ID_CLIENTE=" + ID_CLIENTE + "&o=" + ID_ORG_VTA + "&c=" + ID_CANAL_DISTRIB + "&v=" + ID_VENDEDOR);
            }
        }


    }
}